;*******************************************************************************************
; kernel_header_64.asm
; header to kernel setup routine
;
; Author: Aidan Goddard 23/4/13
;*******************************************************************************************

global	entry

extern	kmain
extern	Printf

;*******************************************************************************************
section .text


entry:
	; setup stack at 0x100010000
	mov	rsp, 0x100010000
	mov	rax, 0
	mov	ss, ax

	; execute main function
	mov 	rax, kmain
	call	rax

	; check return value from main
	or	rax, rax
	jnz	.error

	; return value is 0 (all went well)
	; enable interrupts (so that scheduler can take over)
	sti

	; halt loop
.halt:
	hlt
	jmp	.halt

.error:
	; kernel error section
	mov		rdi, msgErrorString
	mov		rsi, rax
	mov 	rax, Printf
	call	rax
	hlt

;*******************************************************************************************
section .rodata

msgErrorString	db 10,"KERNEL INIT ERROR OCCURED: 0x%x",10,"SYSTEM HALTING",0
